package com.bskj.bms.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.bskj.bms.bean.req.BillListReqVo;
import com.bskj.bms.bean.vo.AllParkinfoVo;
import com.bskj.bms.cache.PropertyCustCache;
import com.bskj.bms.service.IBmsIncomeService;
import com.bskj.common.bean.model.AggregateAmountsVo;
import com.bskj.common.bean.model.FindSumChargeVo;
import com.bskj.common.bean.model.ParkInfo;
import com.bskj.common.bean.model.ParkingLotCustomer;
import com.bskj.common.bean.model.ParkingShop;
import com.bskj.common.bean.model.UserCoupon;
import com.bskj.common.dao.ParkInfoDao;
import com.bskj.common.dao.ParkingLotCustomerDao;
import com.bskj.common.dao.ParkingShopDao;
import com.bskj.common.dao.UserCouponDao;
import com.bskj.common.dao.core.BaseDao;
import com.bskj.common.dao.core.FindRequestEntity;
import com.bskj.common.dao.core.OrderEnum;
import com.bskj.common.dao.core.Page;
import com.bskj.common.dao.core.WhereEnum;
import com.bskj.common.enums.ParkChargeTypeEnum;
import com.bskj.common.service.impl.BaseServiceImpl;
import com.bskj.common.utils.DateTimeUtil;
import com.bskj.common.utils.StringUtils;

@Service
public class BmsIncomeServiceImpl extends BaseServiceImpl<ParkInfo> implements IBmsIncomeService {
	@Autowired
	private ParkInfoDao parkInfoDao;
	
	@Autowired
	private UserCouponDao userCouponDao;
	
	@Autowired
	private ParkingShopDao parkingShopDao;
	
	@Autowired
	private PropertyCustCache propertyCustCache;
	
	@Autowired
	private ParkingLotCustomerDao parkingLotCustomerDao;
	
	@Override
	public BaseDao<ParkInfo> getBaseDao() {
		return parkInfoDao;
	}

	@Override
	public Page<AllParkinfoVo> pageIncome(BillListReqVo req) throws Exception {
		FindRequestEntity findReq = new FindRequestEntity();
		findReq.addWhere("park_lot_custimer_id", WhereEnum.EQ,req.getCustId());
		if (!StringUtils.isBank(req.getEntNo())) {
			findReq.addWhere("charge_type", WhereEnum.EQ,req.getEntNo());
		}else {
			findReq.addWhere("charge_type", WhereEnum.NE,ParkChargeTypeEnum.NOT_PAY.getKey());
		}

		if (!StringUtils.isBank(req.getStartTime())) {
			findReq.addWhere("out_time", WhereEnum.GE, DateTimeUtil.string2Date(req.getStartTime()));
		}
		findReq.addWhere("charge", WhereEnum.GT, 0);
		if (!StringUtils.isBank(req.getEndTime())) {
			findReq.addWhere("out_time", WhereEnum.LE, DateTimeUtil.string2Date(req.getEndTime()));
		}
		
		if (!StringUtils.isBank(req.getBoxNo())) {
			findReq.addWhere("box_no", WhereEnum.EQ,req.getBoxNo());
		}
		
		if (!StringUtils.isBank(req.getCarNo())) {
			findReq.addWhere("car_no", WhereEnum.LIKE, "%" + req.getCarNo() + "%");
		}
		
		findReq.addOrder("out_time", OrderEnum.DESC);
		findReq.setPage(req.getPageNo(), req.getPageSize());
		Page<ParkInfo> parkinfo=page(findReq);
		List<ParkInfo> parkinfolist =((Page<ParkInfo>) parkinfo).getList();
		Page<AllParkinfoVo> resultPage = new Page<>();
		resultPage.setTotalRow(parkinfo.getTotalRow());
		resultPage.setPage(parkinfo.getPage());
		resultPage.setPageSize(parkinfo.getPageSize());
		List<AllParkinfoVo> allParkinfoVolist=new ArrayList<>();
		if(parkinfolist!=null&&parkinfolist.size()>0){
			for (ParkInfo parkInfo2 : parkinfolist) {
				AllParkinfoVo allParkinfoVo=new AllParkinfoVo();
				allParkinfoVo=allParkinfoVo.getAllParkinfoVo(parkInfo2);
				UserCoupon userCoupon=userCouponDao.get(parkInfo2.getUserCouponId());
				if (userCoupon!=null) {
					ParkingShop parkingShop=parkingShopDao.get(userCoupon.getShopId());
					if(parkingShop == null){
						allParkinfoVo.setCouponMeg("已删除的商家", userCoupon.getMoney()/10,userCoupon.getCouponMoneyType());
					}else {
						allParkinfoVo.setCouponMeg(parkingShop.getName(), userCoupon.getMoney()/10,userCoupon.getCouponMoneyType());
					}
				}else {
					allParkinfoVo.setCouponMeg("无");
				}
				allParkinfoVolist.add(allParkinfoVo);
			}
		}
		resultPage.setList(allParkinfoVolist);
		return resultPage;
	}

	@Override
	public List<AggregateAmountsVo> FindAggregateAmount(BillListReqVo req) throws Exception {
		List<AggregateAmountsVo> aggregateAmounts=new ArrayList<>();
		FindSumChargeVo findSumChargeVo=new FindSumChargeVo();
		List<Integer> custList=new ArrayList<>();
		if(req.getCustId() ==0 ){
			custList=propertyCustCache.getCustId(req.getPropId());
		}else {
			custList.add(req.getCustId());
		}
		findSumChargeVo.setCustId(custList);
		findSumChargeVo.setStartTime(req.getStartTime());
		findSumChargeVo.setEndTime(req.getEndTime());
		findSumChargeVo.setBoxNo(req.getBoxNo());
		findSumChargeVo.setChargeType(ParkChargeTypeEnum.CASH_PAY.getKey());
		if(findSumChargeVo.getChargeType()==ParkChargeTypeEnum.CASH_PAY.getKey()){//查询现金支付的总金额，以及有多少笔交易
			List<AggregateAmountsVo> list=parkInfoDao.getSumCharge(findSumChargeVo);
			if(list.size()==0){
				AggregateAmountsVo aggregateAmountsVo=new AggregateAmountsVo();
				aggregateAmountsVo.setChargeType(ParkChargeTypeEnum.CASH_PAY.getKey());
				aggregateAmountsVo.setSum(0);
				aggregateAmountsVo.setCount(0);
				aggregateAmounts.add(aggregateAmountsVo);
			}else {
				for (AggregateAmountsVo aggregateAmountsVo : list) {
					aggregateAmounts.add(aggregateAmountsVo);
				}
			}
		}
		findSumChargeVo.setChargeType(ParkChargeTypeEnum.PLATFROM_PAY.getKey());
		if(findSumChargeVo.getChargeType()==ParkChargeTypeEnum.PLATFROM_PAY.getKey()){//查询网络支付的总金额，以及有多少笔交易
			List<AggregateAmountsVo> list=parkInfoDao.getSumCharge(findSumChargeVo);
			if(list.size()==0){
				ParkingLotCustomer parkingLotCustomer=parkingLotCustomerDao.get(req.getCustId());
				AggregateAmountsVo aggregateAmountsVo=new AggregateAmountsVo();
				if(parkingLotCustomer != null){
					aggregateAmountsVo.setRate(Double.parseDouble(parkingLotCustomer.getRate()));
				}
				aggregateAmountsVo.setChargeType(ParkChargeTypeEnum.PLATFROM_PAY.getKey());
				aggregateAmountsVo.setSum(0);
				aggregateAmountsVo.setCount(0);
				aggregateAmounts.add(aggregateAmountsVo);
			}else {
				for (AggregateAmountsVo aggregateAmountsVo : list) {
					ParkingLotCustomer parkingLotCustomer=parkingLotCustomerDao.get(req.getCustId());
					if(parkingLotCustomer != null){
						aggregateAmountsVo.setRate(Double.parseDouble(parkingLotCustomer.getRate()));
					}
					aggregateAmounts.add(aggregateAmountsVo);
				}
			}
		}
		AggregateAmountsVo aggregate=new AggregateAmountsVo();
		Integer sum=0;
		Integer count=0;
		for (Integer cust : custList) {
			FindRequestEntity findReq=new FindRequestEntity();//查询优惠卷的使用情况
			findReq.addWhere("park_lot_custimer_id", WhereEnum.EQ, cust);
			if(req.getStartTime()!=null){
				findReq.addWhere("out_time", WhereEnum.GT, req.getStartTime());
			}
			if(req.getEndTime()!=null){
				findReq.addWhere("out_time", WhereEnum.LT, req.getEndTime());
			}
			if(!StringUtils.isBank(req.getBoxNo())){
				findReq.addWhere("box_no", WhereEnum.EQ, req.getBoxNo());
			}
			findReq.addWhere("user_coupon_id", WhereEnum.NE, 0);
			count += parkInfoDao.count(findReq.getWhereList());
			List<ParkInfo> parkInfoList=parkInfoDao.find(findReq);
			for (ParkInfo parkInfo : parkInfoList) {
				sum+=parkInfo.getCharge()-parkInfo.getNeedPayCharge();
			}
		}
		aggregate.setCount(count);
		aggregate.setSum(sum);
		aggregate.setChargeType(3);
		aggregateAmounts.add(aggregate);
		return aggregateAmounts;
	}

	@Override
	public List<String> getBoxNoList(Integer custId) throws Exception {
		return parkInfoDao.getBoxNoList(custId);
	}

}
